Repository navigation
[#20567][perf] DFlash: snapshot the prefill context page counts at prepare time - #20568
Draft
lucifer1004 wants to merge 2 commits into
Draft
lucifer1004 wants to merge 2 commits into
lucifer1004 wants to merge 2 commits into
Conversation
… at prepare time With TLLM_DFLASH_PREFILL_CTX_SNAPSHOT=1, DFlashSpecMetadata.prepare() copies the context rows' draft page counts (same formula on the same draft_kv_cache_block_offsets tensor) to pinned memory behind an event, and _store_prefill_context uses that snapshot instead of a blocking .tolist() at the end of the forward. It falls back to the device read when the snapshot does not match the forward. Signed-off-by: Zihua Wu <13583761+lucifer1004@users.noreply.github.com>
…context snapshot on The stub worker binds _snapshot_ctx_block_counts and its state, and the two prepare() tests run with TLLM_DFLASH_PREFILL_CTX_SNAPSHOT off and on. Signed-off-by: Zihua Wu <13583761+lucifer1004@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #20567.
With a DFlash drafter bound to the draft KV cache manager,
_store_prefill_contextreads the context rows' draft page counts with a blocking.tolist()of_ctx_block_countsat the end of every context forward, which waits for the forward's GPU work and exposes the executor's host work between prefill chunks as GPU idle.With
TLLM_DFLASH_PREFILL_CTX_SNAPSHOT=1(new, read at import, default off):DFlashSpecMetadata.prepare()callsDFlashWorker._snapshot_ctx_block_counts(num_contexts): rows[0, num_contexts)ofdraft_kv_cache_block_offsets(already filled byattn_metadata.prepare()on this stream; nothing writes them before the refresh) give the page counts with the same formula as_refresh_ctx_block_tables; they are copied non-blocking into pinned memory and an event is recorded. The offsets tensor is the persistent buffer seen by the previous refresh (_ctx_offsets_src, new)._store_prefill_contexttakes the snapshot (_take_ctx_block_counts) and waits only on its event, which completes when the forward starts. It falls back to the device read when there is no snapshot or it does not match the forward: different row count, different offsets tensor, different draft KV manager or pool index, no block table. Never under CUDA graph capture; the snapshot is consumed once.The values are identical to the device read (same formula, same tensor), so outputs do not change.
Performance (Rubin, TP8 across 2 nodes (MNNVL), Kimi K3 with a DFlash-style drafter, CUDA graphs, single request): the read went from up to about 190 ms of blocking to at most 0.12 ms; end to end, 128K TTFT dropped by about 0.36 s (the idle at chunk boundaries was about 11 ms per chunk before). Accuracy and needle checks unchanged.
#20550 also edits
dflash.py, in different functions (decode-step prologue); the two apply independently.Test Coverage
tests/unittest/_torch/speculative/test_dflash_prefill_ctx_snapshot.py(added tol0_b200.yml): over three successive steps (including empty rows and full rows), the snapshot equals the counts_refresh_ctx_block_tablescomputes from the same tensor, with the GPU still busy when it is taken; it is consumed once; it is rejected when the row count, offsets tensor, manager or pool changed; no snapshot under graph capture.tests/unittest/_torch/speculative/hw_agnostic/test_dflash_dummy_slot.py: the stub worker used by the twoprepare()tests now binds_snapshot_ctx_block_countsand its state, and both tests run with the snapshot off and on.test_dflash_prefill_ctx_snapshot.py: 6 passed (6 failed on main, as expected);hw_agnostic -k dflash: the same pass / fail set as main (the existing failures there need the flash-attn package);TLLM_DFLASH_PREFILL_CTX_SNAPSHOT=1: two extra failures intest_dflash_dummy_slot.py, because its stub worker lacked the new method. Fixed in the test by the latest commit; the rerun is pending.PR Checklist